#!/usr/bin/env python

import telnetlib
import socket
import time

class Netio230ALib(object):

    def __init__(self):
        self.telnet = telnetlib.Telnet("192.168.1.14", 1234)
        try:
            self.telnet.write("login user UsEr111\n")
        except socket.error, e:
            print e
            sys.exit(0)
        time.sleep(0.5)

    def quit(self):
        self.telnet.write("quit\n")
        self.telnet.close()
        self.telnet = None

    def switch(self, port = 1, on = 1):
        self.telnet.write("port %s %s\n" % (port, on))

    def switchOnce(self, port = 1, on = 1):
        self.switch(port, on)
        self.quit()

if __name__ == "__main__":
    import sys
    if len(sys.argv) < 3:
        sys.exit("Give two parameters: Port [1..4] State[0..1]")
    try:
        port = int(sys.argv[1])
        on = int(sys.argv[2])
        if port >= 1 and port <= 4 and on >= 0 and on <= 1:
            t = Netio230ALib()
            t.switchOnce(port, on)
        else:
            sys.exit("Parameters have wrong values")
    except Exception, e:
        print e
